package com.dzzw.Dao;

import com.dzzw.Model.Department;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//部门
public class DepartmentDao {
    private final Connection con = DButils.getCon();
    //新增部门
    public void DepartmentAdd(Department department){
        String sql = "insert into t_department(department_name) values(?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,department.getDepartment_name());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            System.out.println("新增部门数据失败！");
            e.printStackTrace();
        }
    }

    //查询全部部门
    public static List<Department> SelectAllDepartment(){
        List<Department> list = new ArrayList<>();
        String sql = "SELECT * FROM t_department";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Department department = new Department();
                department.setDepartment_id(resultSet.getInt(1));
                department.setDepartment_name(resultSet.getString(2));
                list.add(department);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询部门数据成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //修改部门信息
    public void DepartmentModify(Department department){
        String sql = "update t_department set department_name=? where department_id=?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,department.getDepartment_name());
            preparedStatement.setInt(2,department.getDepartment_id());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            System.out.println("修改部门数据失败！");
            e.printStackTrace();
        }
    }

    //删除部门信息
    public void DepartmentDelete(int department_id){
        String sql = "Delete from t_department where department_id = ?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,department_id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            System.out.println("删除部门数据失败！");
            e.printStackTrace();
        }
    }
}
